package com.sam.im.sms.index.activitys;

import com.sam.im.greendao.DaoSession;
import com.sam.im.sms.R;
import com.sam.im.sms.app.MyApp;
import com.sam.im.sms.base.BaseActivity;
import com.sam.im.sms.beans.ThreadDetailBean;
import com.sam.im.sms.entity.InterceptEntity;
import com.sam.im.sms.index.adapters.RefuseMailboxAdapter;

import android.content.ContextWrapper;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/6.
 */
public class RefuseMailboxActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout mLlLayout;
    private TextView mTvBack;
    private TextView mTvAdd;
    private RecyclerView mRecList;
    private TextView mEmptText;
    private LinearLayoutManager mLayoutManager;
    private RefuseMailboxAdapter mAdapter;
    private List mDatas;
    private List<InterceptEntity> mList;
    private List<InterceptEntity> datas;
    private final static String AUTHORITY = "com.android.peter.provider";
    private final static Uri STUDENT_URI = Uri.parse("content://" + AUTHORITY + "/intercept");
    private String[] projection = new String[]{"id", "address", "type", "read", "body", "date"};

    @Override
    public int getContentViewId() {
        return R.layout.activity_refuse_mailbox;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        Set set = new HashSet();
        mLlLayout = findViewById(R.id.ll_layout);
        mEmptText = findViewById(R.id.empty_text);
        mTvBack = findViewById(R.id.tv_back);
        mTvAdd = findViewById(R.id.tv_add);
        mRecList = findViewById(R.id.rec_list);
        mTvBack.setOnClickListener(this::onClick);
        Cursor cursor = getContentResolver().query(STUDENT_URI, projection, null, null, null);
        praseThreadDetailCursor(cursor);
        datas=new ArrayList<>();
        for (Iterator iterator = mList.iterator(); iterator.hasNext(); ) {
            InterceptEntity entity = (InterceptEntity) iterator.next();
            if (set.add(entity.getAddress())) {
                datas.add(entity);
            }
        }
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecList.setLayoutManager(mLayoutManager);
        mAdapter = new RefuseMailboxAdapter(this, datas);
        mRecList.setAdapter(mAdapter);
    }

    private void praseThreadDetailCursor(Cursor cursor) {
        mList = new ArrayList<>();
        mDatas = new ArrayList<>();
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String type = cursor.getString(cursor.getColumnIndex("type"));
                String read = cursor.getString(cursor.getColumnIndex("read"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                Log.i("lyc::reg", "address" + address + ",body" + body + ",type" + type + ",date" + date + ",contactName");
                InterceptEntity entity;
                entity = new InterceptEntity(address, type, read, body, date);
                mList.add(entity);
            }
        }
        mDatas.add(mList);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }


    private void showNoCallLog(final boolean show) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mEmptText.setVisibility(show ? View.VISIBLE : View.GONE);
            }
        });
    }
}
